﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GN.Pay.Application.GatewayApp;
using GN.Pay.TradeChannel;
using GN.Pay.TradeChannel.YJPay;
using GN.Pay.Utils;

namespace GN.Pay.GatewayWeb.Controllers.Notify
{
    /// <summary>
    /// 易极付
    /// </summary>
    public class YJPayController : APayNotifyController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tradeNotifyAppService"></param>
        public YJPayController(ITradeNotifyAppService tradeNotifyAppService)
            : base(tradeNotifyAppService)
        {

        }


        protected override string ChannelId
        {
            get
            {
                return "YJPay.WebPay";
            }
        }

        public override void OrderPay()
        {
            DictionaryPayNotify();
        }

        protected override NotifyMessage GetPayNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = YJPayServiceBase.GetPayOrderNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        protected override NotifyMessage GetRefundNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = YJPayServiceBase.GetRefundNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        private ActionResult OrderPayReturn(Func<ActionResult> errorActionResult, Func<string, ActionResult> actionResult)
        {
            try
            {
                var msg = GetPayNotifyMessage(Request);
                ITradeChannel channel = GetPayTradeChannel(msg);
                if (channel == null)
                {
                    Logger.Error("付款通知错误,无 " + ChannelId + " 实现");
                    return errorActionResult();
                }
                YJPayServiceBase pay = channel as YJPayServiceBase;
                if (pay == null)
                {
                    Logger.Error(ChannelId + ":未能转换对应的支付类型");
                    return errorActionResult();
                }
                if (!this.IsPayNotify(msg.UniqueNo))
                {
                    pay.ReturnOrderPayNotify(tradeNotifyAppService, (IDictionary<string, string>)msg.Message);
                    this.SetPayNotify(msg.UniqueNo);
                }
                return actionResult(msg.UniqueNo);
            }
            catch (Exception err)
            {
                Logger.Error("易极付出错!", err);
                return errorActionResult();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ActionResult OrderPayReturn()
        {
            return OrderPayReturn(ErrorView, ReturnWaitResult);            
        }

        public override ActionResult OrderPayWapReturn()
        {
            return OrderPayReturn(WapErrorView, ReturnWapWaitResult);
        }

        public override void OrderRefund()
        {
            DictionaryRefundNotify();
        }

       
    }
}